{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "WzCwnbY17x0O",
    "tags": []
   },
   "source": [
    "# 在Jupyter上学习*Think Python*\n",
    "\n",
    "本章对学习Allen B. Downey编写的[*Think Python*](https://greenteapress.com/wp/think-python-3rd-edition)第3版的读者进行Jupyter笔记的介绍。\n",
    "\n",
    "Jupyter笔记是一种包含文本，代码和代码运行结果的文档。你可以像阅读一本书一样阅读笔记，但你也可以运行代码，修改代码并开发新的程序。\n",
    "\n",
    "Jupyter笔记在网页浏览器运行，所以你可以不安装新的软件，但必须连接到Jupyter服务器。\n",
    "\n",
    "你可以安装并运行自己的服务器，但使用像[Colab](https://colab.research.google.com/)的服务对初学者而言更简单。\n",
    "\n",
    "你可以在<https://allendowney.github.io/ThinkPython>找到每章的笔记链接，点击其中一个链接将在Colab打开笔记。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "WzCwnbY17x0O",
    "tags": []
   },
   "source": [
    "如果你在Colab阅读笔记，你会在左上角找到像`CO`的橘色logo。\n",
    "\n",
    "如果你没有在Colab运行本笔记，你可以点击[此处](https://colab.research.google.com/github/AllenDowney/ThinkPython/blob/v3/chapters/jupyter_intro.ipynb)打开。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 译注：本地安装和运行Jupyter\n",
    "\n",
    "对于初学者，建议安装anaconda获得开箱即用的运行环境，包括python环境以及numpy,pandas,scipy,matplotlib,scikit-learn,jupyter等流行的库。对于熟悉conda环境的读者，可以使用miniconda来管理环境。\n",
    "\n",
    "访问[https://www.anaconda.com/download/success]，根据你的操作系统及处理器架构下载对应的anaconda安装包。网页会检测你的设备并提供建议的安装包。\n",
    "\n",
    "### 在linux下安装anaconda\n",
    "\n",
    "linux下使用终端安装anacodna。\n",
    "\n",
    "在家目录打开终端，输入以下命令以安装：\n",
    "\n",
    "```bash\n",
    "wget https://repo.anaconda.com/archive/Anaconda3-2024.10-1-Linux-x86_64.sh # 下载anaconda\n",
    "chmod +x ./Anaconda3-2024.10-1-Linux-x86_64.sh # 给anaconda安装脚本赋予可执行权限\n",
    "./Anaconda3-2024.10-1-Linux-x86_64.sh # 运行anaconda安装脚本\n",
    "# 在交互模式下,回车;\n",
    "# 按q翻页；输入yes同意协议；\n",
    "# 输入你想要放置anaconda的路径，直接回车将默认为～/anaconda3；\n",
    "# 输入yes激活conda环境变量,完成anaconda的安装\n",
    "bash # 重新打开bash，可以看到(base)标识，说明conda的base环境已激活\n",
    "python --version # 检查是否安装了python\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 在windows下安装anaconda\n",
    "\n",
    "1. 下载[windows anaconda安装包](https://repo.anaconda.com/archive/Anaconda3-2024.10-1-Windows-x86_64.exe)，双击运行安装程序\n",
    "2. 点击“Next >”\n",
    "3. 点击“I Agree”\n",
    "4. 点击“Next >”\n",
    "5. 安装目录设置为你想要存放的位置，然后点击“Next >”\n",
    "6. 点击“Install”\n",
    "7. 点击“Next >”\n",
    "8. 点击“Next >”\n",
    "9. 点击“Finish”完成安装\n",
    "\n",
    "### 在本地打开笔记\n",
    "\n",
    "在windows下，可以使用`anaconda navigator`中的图形化界面启动Jupyter notebook应用，也可以点击开始菜单中的`Jupyter notebook`快捷方式；点击想要查看的笔记以进行查看。\n",
    "\n",
    "如果没有跳转到浏览器，`Jupyter notebook`快捷方式中出现“To access the server, open this file in a browser”等信息，需要手动配置浏览器：\n",
    "\n",
    "1. 打开`Anaconda Prompt`，输入`jupyter notebook --generate-config`创建jupyter配置文件\n",
    "2. 输入`notepad .jupyter\\jupyter_notebook_config.py`打开配置文件\n",
    "3. `Ctrl F`搜索`c.ServerApp.use_redirect_file`，将行首的#删除，行尾的True改为False;\n",
    "4. `Ctrl F`搜索`c.ServerApp.open_browser`，将行首的#删除，行尾的False改为True;\n",
    "5. `Ctrl S`保存，再次尝试\n",
    "\n",
    "在linux下，在终端内输入`jupyter notebook`以启动Jupyter服务器。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "WzCwnbY17x0O",
    "tags": []
   },
   "source": [
    "## Jupyter notebook是什么?\n",
    "\n",
    "Jupyter notebook由单元格组成，这些单元格包括文本或者代码。本单元格包含的是文本，下面的单元格包含的是代码。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Hello\n"
     ]
    }
   ],
   "source": [
    "print('Hello') # test"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "WzCwnbY17x0O",
    "tags": []
   },
   "source": [
    "点击上面的单元格以选择它。你应该看到单元格左侧有一个三角形按钮，表示“执行单元格”。按下这个按钮，Jupyter将运行这个单元格的代码并显示结果。\n",
    "\n",
    "当你第一次运行笔记中的代码时，可能需要几秒种进行启动。此外如果某个笔记不是你写的，你可能会受到警告信息。如果你从信任的来源获得和运行该笔记，你可以按下“Run Anyway”。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "WzCwnbY17x0O",
    "tags": []
   },
   "source": [
    "除了按下“执行单元格”按钮外，你也可以按住`Shift`并按下`Enter`来执行光标所在的单元格。\n",
    "\n",
    "如果你在Colab运行这个笔记，你应该能看到单元格左上角有“+ Code”和“+ Text”的按扭。第一个按钮将添加一个代码单元格，第2个将添加一个文本单元格。\n",
    "\n",
    "如果你想试试，选择这个单元格，点击“+ Text”按钮，这个单元格上方将出现一个新的单元格。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "WzCwnbY17x0O",
    "tags": []
   },
   "source": [
    "在那个单元格添加一些文本。你可以使用[Markdown](https://www.markdownguide.org/basic-syntax/)标记语言对你的文本添加格式。\n",
    "\n",
    "文本添加完毕后，按住`Shift`并按下`Enter`将这个单元格的文本进行格式化，然后移动到下一个单元格。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "在任意时刻，Jupyter处于两种模式之一：\n",
    "\n",
    "- 在**命令模式**下，你可以执行影响单元格的操作，例如添加和移除整个单元格；\n",
    "- 在**编辑模式**下，你可以编辑单元格里面的内容。\n",
    "\n",
    "文本单元格的模式很容易区分。在编辑模式下，单元格垂直分割，你编辑的文本在左边，格式化的文本在右边。你也能看到单元格顶部的文本编辑工具；在命令模式下，你只能看到格式化的文本。\n",
    "\n",
    "代码单元格的模式区别比较细微，但如果单元格内有光标，你就处于编辑模式。\n",
    "\n",
    "要从编辑模式切换到命令模式，按下`ESC`按键；反之，按下`Enter`按键。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "完成笔记之后你可以关闭窗口，但你做出的修改可能消失。在修改笔记之后，如果你想要保留修改，打开左上角的文件菜单，可以看到多种保存笔记的方式。\n",
    "\n",
    "- 如果你有Google账户，你可以保存笔记到你的Drive里；\n",
    "- 如果你有Github账户，你可以保存到Github；\n",
    "- 如果你想保存笔记到电脑里，选择“Download”，然后选择“Download .ipnb”。后缀名“.ipynb”表示这是笔记文件，而后缀名“.py”表示只保存笔记中的Python代码；\n",
    "\n",
    "译注：\n",
    "\n",
    "- 本地Jupyter服务器中选择左上角的文件菜单，选择“Save as”保存到你打开Jupyter服务器的位置；\n",
    "- “Download as”则将下载笔记到“下载”目录中；\n",
    "- 已经生成的笔记使用`Ctrl S`会保存并覆盖之前的版本;\n",
    "- 在支持Jupyter notebook的代码编辑器中，按下`Ctrl S`即可快速保存。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## *Think Python*的代码\n",
    "\n",
    "在每章笔记的开始，你将看到有类似以下代码的单元格："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "from os.path import basename, exists\n",
    "\n",
    "def download(url):\n",
    "    filename = basename(url)\n",
    "    if not exists(filename):\n",
    "        from urllib.request import urlretrieve\n",
    "\n",
    "        local, _ = urlretrieve(url, filename)\n",
    "        print(\"Downloaded \" + str(local))\n",
    "    return filename\n",
    "\n",
    "download('https://gitee.com/regentsai/Think_Python_3e_CN/blob/master/thinkpython.py') # 译注：为方便下载，链接替换为gitee源\n",
    "\n",
    "import thinkpython"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "你无须知道这段代码的工作原理，但在本书结束之际，你将理解其中的大部分。正如你可能猜测的那样，它会下载名叫`thinkpython.py`的文件，其中包含本书相关的代码。最后一行导入（import）了这个文件中的代码，我们就可以使用笔记中的代码了。\n",
    "\n",
    "在其他章节，你将看到下载`diagram.py`的代码，该文件用于生成本书的图表；`jupyturtle.py`则用于创建turtle图形。\n",
    "\n",
    "在某些位置将看到单元格以`%%expect`开始。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "ename": "SyntaxError",
     "evalue": "invalid syntax (3827346253.py, line 1)",
     "output_type": "error",
     "traceback": [
      "\u001b[0;36m  Cell \u001b[0;32mIn[4], line 1\u001b[0;36m\u001b[0m\n\u001b[0;31m    abs 42\u001b[0m\n\u001b[0m        ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m invalid syntax\n"
     ]
    }
   ],
   "source": [
    "%%expect SyntaxError\n",
    "\n",
    "abs 42"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "`%%expect`不是Python的一部分，而是Jupyter的\"魔术命令\"，表示这个单元格预期会产生错误。当你看到这个语句，表示这个错误是故意的，通常用来警告你常见的陷阱。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "要了解更多在Colab上运行Jupyter笔记的功能, [点击此处](https://colab.research.google.com/notebooks/basic_features_overview.ipynb).\n",
    "\n",
    "如果你准备好了，[点击此处开始阅读第1章](chap01.ipynb)."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "M9yF11G47x0l",
    "tags": []
   },
   "source": [
    "*Think Python*, 3rd edition.\n",
    "\n",
    "Copyright 2023 [Allen B. Downey](https://allendowney.com)\n",
    "\n",
    "License: [Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International](https://creativecommons.org/licenses/by-nc-sa/4.0/)"
   ]
  }
 ],
 "metadata": {
  "celltoolbar": "Tags",
  "colab": {
   "provenance": []
  },
  "kernelspec": {
   "display_name": "base",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
